﻿
    using Chain.BLL;
    using Chain.Common.DEncrypt;
    using Chain.Model;
    using System;
    using System.Runtime.InteropServices;
    using System.Web;

    public partial class LoginLogic
    {
        private bool _isLoggedOn;
        private DateTime _lastUpdateTime;
        private Chain.Model.SysUser _loginUser;
        private const string CodeName = "LoginState";
        private const int CookieExpires = 0x30;
        private const int LoginStatusExpires = 6;
        private const string TestCookie = "TestCookie";

        private bool CheckLoginStatus()
        {
            string code = this.GetCookie("LoginState");
            if (string.IsNullOrEmpty(code) && (HttpContext.Current.Session["LoginState"] != null))
            {
                code = HttpContext.Current.Session["LoginState"].ToString();
            }
            if (!string.IsNullOrEmpty(code))
            {
                Chain.Model.SysUser model = this.Decode(code, out this._lastUpdateTime);
                if ((model != null) && (this._lastUpdateTime > DateTime.Now.AddHours(-6.0)))
                {
                    this.IsLoggedOn = true;
                    this.LoginUser = model;
                    if (this._lastUpdateTime.AddMinutes(30.0) < DateTime.Now)
                    {
                        this._lastUpdateTime = DateTime.Now;
                        code = this.Encode(model, this._lastUpdateTime);
                        this.LoginIn(code);
                    }
                    return true;
                }
            }
            return false;
        }

        private bool CheckTestCookie()
        {
            return (HttpContext.Current.Request.Cookies["TestCookie"] != null);
        }

        public void CreateTestCookie()
        {
            this.SetCookie("TestCookie", string.Empty, 0x60);
        }

        private Chain.Model.SysUser Decode(string Code, out DateTime CheckTime)
        {
            Chain.Model.SysUser model = new Chain.Model.SysUser();
            CheckTime = DateTime.Now.AddDays(-1.0);
            Code = DESEncrypt.Decrypt(Code);
            string[] temp = Code.Split(new char[] { '|' });
            if (temp.Length == 6)
            {
                model.UserID = Convert.ToInt32(temp[0]);
                model.UserAccount = temp[1];
                model.UserName = temp[2];
                model.UserGroupID = Convert.ToInt32(temp[3]);
                model.UserShopID = Convert.ToInt32(temp[4]);
                CheckTime = Convert.ToDateTime(temp[5]);
                return model;
            }
            return null;
        }

        private string Encode(Chain.Model.SysUser User, DateTime UpdateTime)
        {
            return DESEncrypt.Encrypt(string.Format("{0}|{1}|{2}|{3}|{4}|{5}", new object[] { User.UserID, User.UserAccount, User.UserName, User.UserGroupID, User.UserShopID, UpdateTime }));
        }

        private string GetCookie(string Name)
        {
            if (HttpContext.Current.Request.Cookies[Name] != null)
            {
                return HttpContext.Current.Request.Cookies[Name].Value;
            }
            return string.Empty;
        }

        public bool Login(string Account, string Pwd)
        {
            Chain.Model.SysUser model = new Chain.BLL.SysUser().CheckUserLogin(Account, DESEncrypt.Encrypt(Pwd));
            if ((model != null) && !model.UserLock)
            {
                Chain.Model.SysShop shop = new Chain.BLL.SysShop().GetModel(model.UserShopID);
                if ((shop != null) && !shop.ShopState)
                {
                    this.IsLoggedOn = true;
                    this.LoginUser = model;
                    this._lastUpdateTime = DateTime.Now;
                    string code = this.Encode(model, this._lastUpdateTime);
                    this.LoginIn(code);
                    return true;
                }
            }
            return false;
        }

        private void LoginIn(string Code)
        {
            if (this.CheckTestCookie())
            {
                this.CreateTestCookie();
                this.SetCookie("LoginState", Code, 0x30);
                PubFunction.SaveSysLog(this.LoginUser.UserID, 4, "系统登录", "用户登录系统，账号：[" + this.LoginUser.UserAccount + "] 姓名：[" + this.LoginUser.UserName + "]", this.LoginUser.UserShopID, DateTime.Now, PubFunction.ipAdress);
            }
            else
            {
                HttpContext.Current.Session["LoginState"] = Code;
                PubFunction.SaveSysLog(this.LoginUser.UserID, 4, "系统登录", "用户登录系统，账号：[" + this.LoginUser.UserAccount + "] 姓名：[" + this.LoginUser.UserName + "]", this.LoginUser.UserShopID, DateTime.Now, PubFunction.ipAdress);
            }
        }

        public bool LoginOut()
        {
            this.SetCookie("LoginState", string.Empty, -1);
            HttpContext.Current.Session["LoginState"] = null;
            return true;
        }

        public static LoginLogic LoginStatus()
        {
            LoginLogic login = new LoginLogic();
            login.CheckLoginStatus();
            return login;
        }

        private void SetCookie(string Name, string Value, int Expires)
        {
            HttpCookie cookie = new HttpCookie(Name, Value);
            cookie.Expires = DateTime.Now.AddHours((double) Expires);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public bool IsLoggedOn
        {
            get
            {
                return this._isLoggedOn;
            }
            private set
            {
                this._isLoggedOn = value;
            }
        }

        public DateTime LastUpdateTime
        {
            get
            {
                return this._lastUpdateTime;
            }
            private set
            {
                this._lastUpdateTime = value;
            }
        }

        public Chain.Model.SysUser LoginUser
        {
            get
            {
                return this._loginUser;
            }
            private set
            {
                this._loginUser = value;
            }
        }
    }


